<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_language('member', 'profile');
		$this->load->module_language('member', 'registration');
		$this->load->module_model('member', 'profile_model', 'profile');
		$this->access->restrict();
		$this->user = $this->access->get_user();
		$this->load->library('basic_library');
	}
 
	public function show() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'View Profile User';
		$data['description'] = 'View User Profile';
		//get info of user
		$this->_template['page']	= 'users/profile/show';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function edit() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Edit User Profile';
		$data['description'] = 'Here you can edit User Profile';
		$this->load->library('validation');
		$this->load->helper('form');
		$lang_working = load_language_working();

		// validation
		$this->form_validation->set_rules('newpassword','lang:form_newpassword','trim');
		$this->form_validation->set_rules('renewpassword','lang:form_renew_password','trim|matches[newpassword]');
		$this->form_validation->set_rules('currentpassword','lang:form_current_password','check_password|trim|required');
		$this->form_validation->set_rules('fullname','lang:form_fullname','required|max_length[50]');
		$this->form_validation->set_rules('address','lang:form_address','max_length[50]');
		$this->form_validation->set_rules('yahoo','lang:form_fullname','max_length[32]');
		$this->form_validation->set_rules('skype','lang:form_skype','max_length[32]');				
		$this->form_validation->set_error_delimiters('', '<br />');
		
		// initialize upload photo
		$config['upload_path'] = './uploads/avatars';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		$data['userinfo'] = $this->profile->get_user_info($this->user->id);
		
		if($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name'] == '')
			{
				
				$this->profile->update_profile($this->user->id);
				$this->session->set_flashdata('message', lang('successfully_update_profile'));	
				redirect('member/profile/show', 'refresh');	
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					redirect($this->uri->uri_string());
					
					//$data['ItemID'] = 'User';
					//$data['title'] = 'Edit User Profile';
					//$data['description'] = 'Here you can edit User Profile';
		
					//$data = array('loi' => $this->upload->display_errors());
					//$data['userinfo'] = $this->profile->get_user_info($this->user->id);
					//$this->_template['page']	= 'users/profile/edit';
					//$this->system_library->load($this->_template['page'], $data);
				}
				else
				{
					$this->profile->update_profile($this->user->id);
					$this->session->set_flashdata('message', lang('successfully_update_profile'));	
					redirect('member/profile/show', 'refresh');	
				}
			}
		}
		 
		
		$this->_template['page']	= 'users/profile/edit';
		$this->system_library->load($this->_template['page'], $data);
		
	}
	
	function check_pass()
	{
		if($this->profile->check_pass())
        {
            $this->form_validation->set_message('check_pass','Existing Password Incorrect');
            return false;
        }
        else
        {
            
            return true;
        }
	}
			
}

/* End of file welcome.php */
/* Location: ./application/controllers/profile.php */